package com.ctu.gzero.utils 
{
	import flash.events.*;
	import flash.ui.*;
	
	import mx.controls.TextArea;
	import mx.controls.Tree;
	import mx.controls.listClasses.*;
	import mx.events.*;
	
	public class TreeManager 
	{
		private var _console:TextArea = null;
		private var _contextMenu:ContextMenu = null;
		private var _tree:Tree = null;
		private var _enabled:Boolean = false;
		
		public function TreeManager(tree:Tree, obj:TextArea) 
		{
			_tree = tree;
			_console = obj;		
			createContextMenu();			
		}
		
		private function createContextMenu():void 
		{
			_contextMenu = new ContextMenu(); 
            _contextMenu.addEventListener(ContextMenuEvent.MENU_SELECT,  contextMenuSelectHandler);            
        }
        
		public function enableTreeDebug():void 
		{
			if(_tree == null) 
			{
				return;
			} 
			
			_tree.dragEnabled=true;
			_tree.dragMoveEnabled=true;
        	_tree.dropEnabled=true;
        	_tree.contextMenu=_contextMenu;
        	
			_tree.addEventListener(DragEvent.DRAG_ENTER,    onDragEnter);
			_tree.addEventListener(DragEvent.DRAG_OVER,     onDragOver);
			_tree.addEventListener(DragEvent.DRAG_DROP,     onDragDrop);
			_tree.addEventListener(DragEvent.DRAG_COMPLETE, onDragComplete);	
			
		}
		
		public function disableTreeDebug():void 
		{
			if(_tree == null)
			{
				return;
			}
			
			_tree.dragEnabled=false;
			_tree.dragMoveEnabled=false;
        	_tree.dropEnabled=false;
        	
			_tree.removeEventListener(DragEvent.DRAG_ENTER,    onDragEnter);
			_tree.removeEventListener(DragEvent.DRAG_OVER,     onDragOver);
			_tree.removeEventListener(DragEvent.DRAG_DROP,     onDragDrop);
			_tree.removeEventListener(DragEvent.DRAG_COMPLETE, onDragComplete);
			
		}
		
        private function contextMenuSelectHandler(event:ContextMenuEvent):void 
        {
        	_contextMenu.customItems = new Array();
                    
            if( _tree.selectedItem != null )
            {
            	var nodeType:String = _tree.selectedItem.@type;
            	
            	if(nodeType == "DomElement")
            	{
            		var addParaElementMenu:ContextMenuItem = new ContextMenuItem('Add ParaElement');
                    
                    addParaElementMenu.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, onAddParaElement);
                    
                    _contextMenu.customItems.push(addParaElementMenu);
            	}
                else if( nodeType == "ParaElement" )
                {
                    var addTxtRunMenu:ContextMenuItem = new ContextMenuItem('Add TxtRun');
                    var addObjectRunMenu:ContextMenuItem = new ContextMenuItem('Add ObjectRun');
                    var removeParaElementMenu:ContextMenuItem = new ContextMenuItem("Remove ParaElement");
                    
                    addTxtRunMenu.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, onAddTxtRun);
                    addObjectRunMenu.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, onAddObjectRun);
                    removeParaElementMenu.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, onRemoveParaElement);
                    
                    _contextMenu.customItems.push(addTxtRunMenu);
                    _contextMenu.customItems.push(addObjectRunMenu);
                     _contextMenu.customItems.push(removeParaElementMenu);
                } 
                else if(nodeType == "TxtRun") 
                {
                	var removeTextRunMenu:ContextMenuItem = new ContextMenuItem("Remove TxtRun");
                	removeTextRunMenu.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, onRemoveTextRun);
                	
                	_contextMenu.customItems.push(removeTextRunMenu);
                } 
                else if(nodeType == "ObjectRun") 
                {
                	var removeObjectRunMenu:ContextMenuItem = new ContextMenuItem("Remove ObjectRun");
                	removeObjectRunMenu.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, onRemoveObjectRun);
                	
                	_contextMenu.customItems.push(removeObjectRunMenu);
                }
                
                _contextMenu.hideBuiltInItems();
            }
        	      
        }
        
        private function onAddParaElement(event:ContextMenuEvent):void
        {
        	var selectedNode:XML;
        	var paraXML:XML = <ParaElement />;
        	var str:String = "new insert TxtRun";
			var textRunXML:XML = <TextRun ></TextRun>;
			
        	selectedNode = _tree.selectedItem as XML;
			paraXML.@label = "ParaElement";
			paraXML.@id = "";
			paraXML.@type = "ParaElement";
			paraXML.@textLength = 0;
			paraXML.@absoluteStart = 0;

			textRunXML.@label = "TextRun";
			textRunXML.@id = "";
			textRunXML.@type = "TxtRun";
			textRunXML.@absoluteStart = 0;
			textRunXML.@textLength = str.length;
			textRunXML.@fontSize = 20;
			textRunXML.@lineHeight = "150%";
			textRunXML.@text = str;
			
			paraXML.appendChild(textRunXML);
			selectedNode.appendChild(paraXML);
        }
        
        private function onAddTxtRun(event:ContextMenuEvent):void
        {
        	var selectedNode:XML;
        	var str:String = "new insert TxtRun";
			var textRunXML:XML = <TextRun ></TextRun>;			
        	
			selectedNode = _tree.selectedItem as XML;
			textRunXML.@label = "TextRun";
			textRunXML.@id = "";
			textRunXML.@type = "TxtRun";
			textRunXML.@absoluteStart = 0;
			textRunXML.@textLength = str.length;
			textRunXML.@fontSize = 20;
			textRunXML.@lineHeight = "150%";
			textRunXML.@text = str;
			selectedNode.appendChild(textRunXML);
        }
        
        private function onAddObjectRun(event:ContextMenuEvent):void
        {
        	var selectedNode:XML;
			var objectRunXML:XML = <ObjectRun ></ObjectRun>;	
        	
			selectedNode = _tree.selectedItem as XML;
			objectRunXML.@label = "ObjectRun";
			objectRunXML.@id = "";
			objectRunXML.@type = "ObjectRun";
			objectRunXML.@absoluteStart = 0;
			objectRunXML.@textLength = 0;
			objectRunXML.@width = 50;
			objectRunXML.@height = 50;
			objectRunXML.@backgroundColor = 0xeeeeee;
			objectRunXML.@alpha = 1;
			selectedNode.appendChild(objectRunXML);
        }
        
        private function onRemoveParaElement(event:ContextMenuEvent):void
        {
        	
        }
        
        private function onRemoveTextRun(event:ContextMenuEvent):void
        {
        	
        }
        
        private function onRemoveObjectRun(event:ContextMenuEvent):void
        {
        	
        }
        
		// Drag functionalities
        public function onDragEnter(event:DragEvent):void 
        {
			trace("Drag started : object " + event.currentTarget);	
			var selectedNode:XML;
			selectedNode=Tree(event.target).selectedItem as XML;
						
            var attNamesList:XMLList = selectedNode.@*;
            var id:String = selectedNode.@id;
			
		
			if(_console) 
			{
				_console.text = "Drag started : item " + id+"\n";
			}			
		}
		
		public function onDragDrop(event:DragEvent):void 
		{
			trace("Droped : object " + event.currentTarget);
			if(_console) 
			{
				_console.text += "Droped : object " + event.currentTarget+"\n"
			}
		}
		
		public function onDragOver(event:DragEvent):void 
		{
			trace("Drag over : object " + event.currentTarget);
			if(_console) 
			{
				_console.text += "Drag over : object " + event.currentTarget+"\n"
			}
		}
		
		public function onDragComplete(event:DragEvent):void 
		{
			trace("Drag completed : object " + event.currentTarget);
			if(_console) 
			{
				_console.text += "Drag completed : object " + event.currentTarget+"\n";
			}
		}
		
	}
}